<!DOCTYPE html><html><head><title>README</title><link href="http://www.jboss.org/jdf/stylesheets/documentation.css" rel="stylesheet"></link><link href="http://www.jboss.org/jdf/stylesheets/pygments.css" rel="stylesheet"></link></head><body>
<h1><a id="tasks-testing-jpa-with-arquillian" class="anchor" href="#tasks-testing-jpa-with-arquillian"><span class="anchor-icon"></span></a>tasks: Testing JPA with Arquillian</h1>

<p>Author: Oliver Kiss, Lukas Fryc<br/>
Level: Intermediate<br/>
Technologies: JPA, Arquillian<br/>
Summary: Demonstrates testing JPA using Arquillian<br/>
Target Product: EAP<br/>
Product Versions: EAP 6.1, EAP 6.2<br/>
Source: <a href="https://github.com/jboss-developer/jboss-eap-quickstarts/">https://github.com/jboss-developer/jboss-eap-quickstarts/</a>  </p>

<h2><a id="what-is-it" class="anchor" href="#what-is-it"><span class="anchor-icon"></span></a>What is it?</h2>

<p>This project demonstrates how to use JPA 2.0 in Red Hat JBoss Enterprise Application Platform. </p>

<p>It includes a persistence unit and some sample persistence code to introduce you database access in enterprise Java. </p>

<p>It does not contain an user interface layer. The purpose of the project is to show you how to test JPA with Arquillian.</p>

<p><em>Note: This quickstart uses the H2 database included with JBoss EAP 6. It is a lightweight, relational example datasource that is used for examples only. It is not robust or scalable and should NOT be used in a production environment!</em></p>

<h2><a id="system-requirements" class="anchor" href="#system-requirements"><span class="anchor-icon"></span></a>System requirements</h2>

<p>The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 6.1 or later. </p>

<p>All you need to build this project is Java 6.0 (Java SDK 1.6) or later, Maven 3.0 or later.</p>

<p><em>Note: This quickstart uses the H2 database included with JBoss EAP 6. It is a lightweight, relational example datasource that is used for examples only. It is not robust or scalable and should NOT be used in a production environment!</em></p>

<h2><a id="configure-maven" class="anchor" href="#configure-maven"><span class="anchor-icon"></span></a>Configure Maven</h2>

<p>If you have not yet done so, you must <a href="../README.html#configure-maven">Configure Maven</a> before testing the quickstarts.</p>

<h2><a id="start-the-jboss-server" class="anchor" href="#start-the-jboss-server"><span class="anchor-icon"></span></a>Start the JBoss Server</h2>

<ol>
<li>Open a command line and navigate to the root of the JBoss server directory.</li>
<li><p>The following shows the command line to start the server:</p>
<div class="highlight"><pre><span class="n">For</span> <span class="n">Linux</span><span class="o">:</span>   <span class="n">JBOSS_HOME</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">standalone</span><span class="p">.</span><span class="n">sh</span>
<span class="n">For</span> <span class="n">Windows</span><span class="o">:</span> <span class="n">JBOSS_HOME</span><span class="err">\</span><span class="n">bin</span><span class="err">\</span><span class="n">standalone</span><span class="p">.</span><span class="n">bat</span>
</pre></div></li>
</ol>

<h2><a id="run-the-arquillian-tests-" class="anchor" href="#run-the-arquillian-tests-"><span class="anchor-icon"></span></a>Run the Arquillian Tests </h2>

<p>This quickstart provides Arquillian tests. By default, these tests are configured to be skipped as Arquillian tests require the use of a container. </p>

<p><em>NOTE: The following commands assume you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See <a href="../README.html#run-the-arquillian-tests">Run the Arquillian Tests</a> for complete instructions and additional options.</em></p>

<ol>
<li>Make sure you have started the JBoss Server as described above.</li>
<li>Open a command line and navigate to the root directory of this quickstart.</li>
<li><p>Type the following command to run the test goal with the following profile activated:</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">clean</span> <span class="n">test</span> <span class="o">-</span><span class="n">Parq</span><span class="o">-</span><span class="n">jbossas</span><span class="o">-</span><span class="n">remote</span> 
</pre></div></li>
</ol>

<h2><a id="run-tests-from-jbds" class="anchor" href="#run-tests-from-jbds"><span class="anchor-icon"></span></a>Run tests from JBDS</h2>

<p>To be able to run the tests from JBDS, first set the active Maven profile in project properties to be either &lsquo;arq-jbossas-managed&rsquo; for running on
managed server or &lsquo;arq-jbossas-remote&rsquo; for running on remote server.</p>

<p>To run the tests, right click on the project or individual classes and select Run As &ndash;> JUnit Test in the context menu.</p>

<h2><a id="investigate-the-console-output" class="anchor" href="#investigate-the-console-output"><span class="anchor-icon"></span></a>Investigate the Console Output</h2>

<h3><a id="maven" class="anchor" href="#maven"><span class="anchor-icon"></span></a>Maven</h3>

<p>Maven prints summary of performed tests into the console:</p>
<div class="highlight"><pre><span class="o">-------------------------------------------------------</span>
 <span class="n">T</span> <span class="n">E</span> <span class="n">S</span> <span class="n">T</span> <span class="n">S</span>
<span class="o">-------------------------------------------------------</span>
<span class="n">Running</span> <span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">tasks</span><span class="p">.</span><span class="n">TaskDaoTest</span>
<span class="nl">log4j:</span><span class="n">WARN</span> <span class="n">No</span> <span class="n">appenders</span> <span class="n">could</span> <span class="n">be</span> <span class="n">found</span> <span class="k">for</span> <span class="n">logger</span> <span class="p">(</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">logging</span><span class="p">).</span>
<span class="nl">log4j:</span><span class="n">WARN</span> <span class="n">Please</span> <span class="n">initialize</span> <span class="n">the</span> <span class="n">log4j</span> <span class="n">system</span> <span class="n">properly</span><span class="p">.</span>
<span class="n">Tests</span> <span class="n">run</span><span class="o">:</span> <span class="mi">5</span><span class="p">,</span> <span class="n">Failures</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Errors</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Skipped</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Time</span> <span class="n">elapsed</span><span class="o">:</span> <span class="mf">2.31</span> <span class="n">sec</span>
<span class="n">Running</span> <span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">tasks</span><span class="p">.</span><span class="n">UserDaoTest</span>
<span class="n">Tests</span> <span class="n">run</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span> <span class="n">Failures</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Errors</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Skipped</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Time</span> <span class="n">elapsed</span><span class="o">:</span> <span class="mf">1.084</span> <span class="n">sec</span>

<span class="n">Results</span> <span class="o">:</span>

<span class="n">Tests</span> <span class="n">run</span><span class="o">:</span> <span class="mi">8</span><span class="p">,</span> <span class="n">Failures</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Errors</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Skipped</span><span class="o">:</span> <span class="mi">0</span>
</pre></div>
<h3><a id="server-log" class="anchor" href="#server-log"><span class="anchor-icon"></span></a>Server log</h3>

<p>SQL statements generated by Hibernate are written into the server log.</p>

<h4><a id="expected-warnings-and-errors" class="anchor" href="#expected-warnings-and-errors"><span class="anchor-icon"></span></a>Expected warnings and errors</h4>

<p><em>Note:</em> You will see the following warnings and errors in the server log. Hibernate attempts to drop the table and constraints before they are created because the <code>hibernate.hbm2ddl.auto</code> value is set to <code>create-drop</code>. You can ignore these errors.</p>
<div class="highlight"><pre>    <span class="nl">HHH000431:</span> <span class="n">Unable</span> <span class="n">to</span> <span class="n">determine</span> <span class="n">H2</span> <span class="n">database</span> <span class="n">version</span><span class="p">,</span> <span class="n">certain</span> <span class="n">features</span> <span class="n">may</span> <span class="n">not</span> <span class="n">work</span>

    <span class="nl">HHH000389:</span> <span class="n">Unsuccessful</span><span class="o">:</span> <span class="n">alter</span> <span class="n">table</span> <span class="n">Task</span> <span class="n">drop</span> <span class="n">constraint</span> <span class="n">FK_kxfu633bvt1sptgbnkxkrr3qf</span>
    <span class="n">Table</span> <span class="s">&quot;TASK&quot;</span> <span class="n">not</span> <span class="n">found</span><span class="p">;</span> <span class="n">SQL</span> <span class="n">statement</span><span class="o">:</span> <span class="n">alter</span> <span class="n">table</span> <span class="n">Task</span> <span class="n">drop</span> <span class="n">constraint</span> <span class="n">FK_kxfu633bvt1sptgbnkxkrr3qf</span> <span class="p">[</span><span class="mi">42102</span><span class="o">-</span><span class="mi">168</span><span class="p">]</span>
</pre></div>
<h4><a id="example-output-from-the-tests" class="anchor" href="#example-output-from-the-tests"><span class="anchor-icon"></span></a>Example Output from the tests</h4>

<p>Creating the database schema:</p>
<div class="highlight"><pre><span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">770</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">MSC</span> <span class="n">service</span> <span class="n">thread</span> <span class="mi">1</span><span class="o">-</span><span class="mi">2</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">create</span> <span class="n">table</span> <span class="n">Tasks_task</span> <span class="o">(</span><span class="n">id</span> <span class="n">bigint</span> <span class="n">not</span> <span class="kc">null</span><span class="o">,</span> <span class="n">title</span> <span class="n">varchar</span><span class="o">(</span><span class="mi">255</span><span class="o">),</span> <span class="n">owner_id</span> <span class="n">bigint</span><span class="o">,</span> <span class="n">primary</span> <span class="n">key</span> <span class="o">(</span><span class="n">id</span><span class="o">))</span>
<span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">771</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">MSC</span> <span class="n">service</span> <span class="n">thread</span> <span class="mi">1</span><span class="o">-</span><span class="mi">2</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">create</span> <span class="n">table</span> <span class="n">Tasks_user</span> <span class="o">(</span><span class="n">id</span> <span class="n">bigint</span> <span class="n">not</span> <span class="kc">null</span><span class="o">,</span> <span class="n">username</span> <span class="n">varchar</span><span class="o">(</span><span class="mi">255</span><span class="o">),</span> <span class="n">primary</span> <span class="n">key</span> <span class="o">(</span><span class="n">id</span><span class="o">))</span>
<span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">772</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">MSC</span> <span class="n">service</span> <span class="n">thread</span> <span class="mi">1</span><span class="o">-</span><span class="mi">2</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">alter</span> <span class="n">table</span> <span class="n">Tasks_task</span> <span class="n">add</span> <span class="n">constraint</span> <span class="n">FKE61757B62CC79EF1</span> <span class="n">foreign</span> <span class="n">key</span> <span class="o">(</span><span class="n">owner_id</span><span class="o">)</span> <span class="n">references</span> <span class="n">Tasks_user</span>
</pre></div>
<p>Generating ID for a new entity and inserting the entity into the database:</p>
<div class="highlight"><pre><span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">956</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">http</span><span class="o">--</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="o">-</span><span class="mi">8080</span><span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">select</span> <span class="n">tbl</span><span class="o">.</span><span class="na">next_val</span> <span class="n">from</span> <span class="n">hibernate_sequences</span> <span class="n">tbl</span> <span class="n">where</span> <span class="n">tbl</span><span class="o">.</span><span class="na">sequence_name</span><span class="o">=?</span> <span class="k">for</span> <span class="n">update</span>
<span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">957</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">http</span><span class="o">--</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="o">-</span><span class="mi">8080</span><span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">insert</span> <span class="n">into</span> <span class="n">hibernate_sequences</span> <span class="o">(</span><span class="n">sequence_name</span><span class="o">,</span> <span class="n">next_val</span><span class="o">)</span>  <span class="n">values</span> <span class="o">(?,?)</span>
<span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">958</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">http</span><span class="o">--</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="o">-</span><span class="mi">8080</span><span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">update</span> <span class="n">hibernate_sequences</span> <span class="kd">set</span> <span class="n">next_val</span><span class="o">=?</span>  <span class="n">where</span> <span class="n">next_val</span><span class="o">=?</span> <span class="n">and</span> <span class="n">sequence_name</span><span class="o">=?</span>
<span class="mi">10</span><span class="o">:</span><span class="mi">16</span><span class="o">:</span><span class="mi">58</span><span class="o">,</span><span class="mi">960</span> <span class="n">INFO</span>  <span class="o">[</span><span class="n">stdout</span><span class="o">]</span> <span class="o">(</span><span class="n">http</span><span class="o">--</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="o">-</span><span class="mi">8080</span><span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="n">Hibernate</span><span class="o">:</span> <span class="n">insert</span> <span class="n">into</span> <span class="n">Tasks_user</span> <span class="o">(</span><span class="n">username</span><span class="o">,</span> <span class="n">id</span><span class="o">)</span> <span class="n">values</span> <span class="o">(?,</span> <span class="o">?)</span>
</pre></div>
<h2><a id="run-the-quickstart-in-jboss-developer-studio-or-eclipse" class="anchor" href="#run-the-quickstart-in-jboss-developer-studio-or-eclipse"><span class="anchor-icon"></span></a>Run the Quickstart in JBoss Developer Studio or Eclipse</h2>

<p>You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see <a href="../README.html#use-jboss-developer-studio-or-eclipse-to-run-the-quickstarts">Use JBoss Developer Studio or Eclipse to Run the Quickstarts</a> </p>

<h2><a id="debug-the-application" class="anchor" href="#debug-the-application"><span class="anchor-icon"></span></a>Debug the Application</h2>

<p>If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.</p>
<div class="highlight"><pre>    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">sources</span>
    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">resolve</span> <span class="o">-</span><span class="n">Dclassifier</span><span class="o">=</span><span class="n">javadoc</span>
</pre></div></body></html>
